// pages/index/guessing_competition/guessing_list_details/guessing_list_details.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        guessing_list: {},
        show_yes: false,
        show_no: false,
        integral: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id
        })
        this.getGuessingListDetails()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getGuessingListDetails: function (e) {
        http.get("questions/open/getAppletPaper", {
            paperid: this.data.id,
            applet_user_id: app.globalData.userInfo.applet_user_id,
            state: 1
        }).then(res => {
            if (res.data.code == 200) {
                res.data.data.array.forEach((item, index) => {
                    item.list.forEach((item1, index1) => {
                        item1.letter = String.fromCharCode(65 + index1);
                        if (item1.is_answer == 1) {
                            item.answer += String.fromCharCode(65 + index1) + "　";
                        }
                    })
                })
                this.setData({
                    guessing_list: res.data.data.array[0]
                })
            }
        })
    },
    onAnswer: function (e) {
        const questionsid = e.currentTarget.dataset.questionsid
        const optionid = e.currentTarget.dataset.optionid
        const list = []
        let yes_total
        let wrong_total
        this.data.guessing_list.list.forEach((item, index) => {
            if (optionid == item.id) {
                yes_total = 1
                wrong_total = 0
            } else {
                wrong_total = 1
                yes_total = 0
            }
        })


        list.push({
            appletUserId: app.globalData.userInfo.applet_user_id,
            nickname: app.globalData.userInfo.nickname,
            paperid: this.data.id,
            questionsid: questionsid,
            optionid: optionid,
            answer: "",
        })
        Dialog.confirm({
            title: '提示',
            message: '确认提交吗？',
        })
            .then(() => {
                http.putJson("questions/open/addAnswerLog", {
                    id: this.data.id,
                    total: 1,
                    yes_total: yes_total,
                    wrong_total: wrong_total,
                    nickname: app.globalData.wxUserInfo.nickName,
                    applet_user_id: app.globalData.userInfo.applet_user_id,
                    area_code: app.globalData.areaCode,
                    questionnaire_type: 3
                }).then(res => {
                    if (res.data.code == 200) {
                        this.setData({
                            integral: res.data.data.integral
                        })
                    }
                })
                http.putJson("questions/open/addQuesAnswer", list)
                    .then(res => {
                        if (res.data.code == 200) {
                            if (yes_total == 1) {
                                this.setData({
                                    show_no: true
                                });
                            } else {
                                this.setData({
                                    show_yes: true
                                });
                            }
                            setTimeout(res => {
                                wx.navigateBack(2)
                            }, 2000)
                        } else {
                            Toast.fail(res.data.msg);
                        }
                    })
            })
            .catch(() => {
                // on cancel
            });
    },
    onClose() {
        this.setData({
            show_yes: false,
            show_no: false
        });
    },
})